package com.aaa.dang.modules.user.Controller;

import com.aaa.dang.common.systemconst.ISystemConst;
import com.aaa.dang.common.util.BaseContrllor;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.user.service.IRoomChangeService;
import com.aaa.dang.modules.user.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.SoundbankResource;
import java.io.*;
import java.util.*;

/**
 * @author JinChaoLiang
 * @calss QY87
 * @description 描述
 * @date 2018/12/14
 */
@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController extends BaseContrllor {

    @Autowired
    private IUserService userService;

    @Value("${fileupload.path}")
    private String filePath;

    @RequestMapping("/toGetAll")
    public String getUserAndRoomInfo(){
        return "/user/getinfo";
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public ResultModel getAll(@RequestParam Map<String,Object> query) {

        ResultModel rm = userService.getList(query);
        return rm;
    }
    @RequestMapping("/getInfoById")
    @ResponseBody
    public Map<String,Object> getInfoById(int id){
        System.out.println(id);
        Map<String,Object> map = userService.getInfoById(id);
        return map;
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultModel login(String username, String psw)
    {

    return userService.login(username,psw);
    }

    /***
     * 手机号唯一性校验
     * @param username
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public ResultModel checkPhone(String username) {
        ResultModel rm = userService.checkPhone(username);
        return rm;
    }

    /****
     *添加用户
     * @param username
     * @param psw
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public ResultModel addUser(String username,String psw) {
        System.out.println(username);
        ResultModel rm = userService.addUser(username,psw);
        return rm;
    }
    /**
     * 手机端用户修改密码
     * @param map
     * @return
     */
    @RequestMapping("/updatepsw")
    @ResponseBody
    public ResultModel updatepsw(@RequestParam Map<String,Object> map){
        System.out.println(map);
        ResultModel rm = userService.updatepsw(map);
        return rm;
    }

    /**
     * 手机端获取部分资料
     * @param username
     * @return
     */
    @RequestMapping("/getUserPartialInfo")
    @ResponseBody
    public ResultModel getUserPartialInfo(String username){
        ResultModel rm = userService.getUserPartialInfo(username);
        return rm;
    }

    /**
     * 手机端完善资料
     * @param map
     * @return
     */
    @RequestMapping("/updateinfo")
    @ResponseBody
    public ResultModel updateinfo(@RequestParam Map<String,Object> map) {
        ResultModel rm = userService.updateinfo(map);
        return rm;
    }


    @RequestMapping("/findpassword")
    @ResponseBody
    public ResultModel findpassword(@RequestParam Map<String,Object> query){

        return userService.findpassword(query);

    }

    /***
     * 获得所有小区
     * @return
     */
    @RequestMapping("/getAllArea")
    @ResponseBody
    public ResultModel getAllArea(){
        System.out.println("成功访问");
        ResultModel rm = userService.getAllArea();
        return  rm;

    }

    @RequestMapping("/getUser")
    @ResponseBody
    public ResultModel getUser(int id){
        return userService.getUser(id);
    }

    /***
     * 根据小区id获得所有楼栋
     * @param id
     * @return
     */
    @RequestMapping("/getBuildingsByAreaId")
    @ResponseBody
    public ResultModel getBuildingsByAreaId(Integer id) {
        ResultModel rm = userService.getBuildingsByAreaId(id);
        return rm;
    }

    /***
     * 根据l楼栋ID获得所有房间
     * @param id
     * @return
     */
    @RequestMapping("/getRoomsByBuildingId")
    @ResponseBody
    public ResultModel getRoomsByBuildingId(Integer id){
        ResultModel rm = userService.getRoomsByBuildingId(id);
        System.out.println(id);
        return rm;
    }
    /***
     * 根据条件查询所有房间
     * @param data
     * @return
     */
    @RequestMapping("/getRoomsByQuery")
    @ResponseBody
    public ResultModel getRoomsByQuery(@RequestParam Map<String,Object> data){
        System.out.println("-----------");
        System.out.println(data);
        System.out.println("-----------");

        ResultModel rm = userService.getRoomsByQuery(data);

        System.out.println(rm);
        return rm;
    }

    /***
     * 用户申请报修
     * @param data
     * @return
     */
    @RequestMapping("/addRepairapply")
    @ResponseBody
    public ResultModel addRepairapply(@RequestParam Map<String,Object> data) {
        System.out.println(data);
        ResultModel rm = userService.addRepairapply(data);
        return rm;
    }

    /***
     * 根据用户id获得该用户的报修记录
     * @param userId
     * @return
     */
    @RequestMapping("/getRepairApplyByUserId")
    @ResponseBody
    public ResultModel getRepairApplyByUserId(Integer userId) {
        System.out.println(userId);
        ResultModel rm = userService.getRepairApplyByUserId(userId);
        return rm;
    }

    /***
     * 根据id修改报修状态
     * @param data
     * @return
     */
    @RequestMapping("/updateStatusById")
    @ResponseBody
    public ResultModel updateStatusById(@RequestParam Map<String,Object> data) {
        ResultModel rm = userService.updateStatusById(data);
        return rm;
    }

    /***
     * 根据用户id获得入住申请记录
     * @param userId
     * @return
     */
    @RequestMapping("/getStayApplyByUserId")
    @ResponseBody
    public ResultModel getStayApplyByUserId(Integer userId) {
        ResultModel rm = userService.getStayApplyByUserId(userId);
        return rm;
    }

    /***
     * 用户撤回入住申请
     * @param data
     * @return
     */
    @RequestMapping("/updateStayApplyStatus")
    @ResponseBody
    public ResultModel updateStayApplyStatus(@RequestParam Map<String,Object> data){
        ResultModel rm = userService.updateStayApplyStatus(data);
        return rm;
    }

    /***
     * 根据用户id获得所有退宿申请记录
     * @param userId
     * @return
     */
    @RequestMapping("/getRetireApply")
    @ResponseBody
    public ResultModel getRetireApply(Integer userId) {
        ResultModel rm = userService.getRetireApply(userId);
        return rm;
    }

    /***
     * 用户退宿撤回
     * @param data
     * @return
     */
    @RequestMapping("/updateRetireApplyStatus")
    @ResponseBody
    public ResultModel updateRetireApplyStatus(@RequestParam Map<String,Object> data) {
        ResultModel rm = userService.updateRetireApplyStatus(data);
        return rm;
    }
    /***
     * 根据用户id获得缴纳租金记录
     * @param userId
     * @return
     */
    @RequestMapping("/getDepositApply")
    @ResponseBody
    public ResultModel getDepositApply(Integer userId) {
        ResultModel rm = userService.getDepositApply(userId);
        return rm;
    }

    /***
     * 撤回押金申请
     * @param map
     * @return
     */
    @RequestMapping("/updateDepositApplyStatus")
    @ResponseBody
    public ResultModel updateDepositApplyStatus(@RequestParam Map<String,Object> map) {
        ResultModel rm = userService.updateDepositApplyStatus(map);
        return rm;
    }

    /***
     * 获得租金申请记录
     * @param userId
     * @return
     */
    @RequestMapping("/getPayRentApply")
    @ResponseBody
    public ResultModel getPayRentApply(Integer userId) {
        ResultModel rm = userService.getPayRentApply(userId);
        return rm;
    }
    /***
     * 撤回租金申请
     * @param map
     * @return
     */
    @RequestMapping("/updatePayRentApplyStatus")
    @ResponseBody
    public ResultModel updatePayRentApplyStatus(@RequestParam Map<String,Object> map) {
        ResultModel rm = userService.updatePayRentApplyStatus(map);
        return rm;
    }

    }
